package com.oms.app.user.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oms.app.sequence.service.impl.SequenceManager;
import com.oms.app.user.model.Group;
import com.oms.app.user.service.IGroupService;
import com.oms.page.PageModel;


/**
 * controller
 * @author wtj
 * @date Sep 10, 2012  10:08:42 PM
 */
@RequestMapping("")
public class GroupAction {
	private IGroupService groupService;
	
	public void setGroupService(IGroupService groupService) {
		this.groupService = groupService;
	}
	/*** 后台管理 BEGIN ***/
	//进入用户管理列表
	@RequestMapping(value="group_groupManagerList", method=RequestMethod.GET)
	public String enterGroupManagerList(HttpServletRequest request){
		return "manager/group/groupManagerList";
	}
	 // 获取用户组列表
	@ResponseBody
	@RequestMapping(value="/group_getList")
	public Map<String, Object> getGroupList(HttpServletRequest request){
		List list =  groupService.getGroupList(request);
		return PageModel.getListToString(list, request);
	}
	 // 用户组设置角色
	@RequestMapping(value="/group_groupSetupRole/{groupId}")
	public String enterGroupSetupRole(@PathVariable String groupId,HttpServletRequest request){
		request.setAttribute("groupId", groupId);
		return "manager/group/groupSetupRole";
	}
	 // 用户组设置用户
	@RequestMapping(value="/group_groupSetupUser")
	public String enterGroupSetupUser(HttpServletRequest request){
		return "manager/group/groupSetupUser";
	}
	 // 用户组设置用户
	@RequestMapping(value="/group_groupAddUser")
	public void groupAddUser(HttpServletRequest request){
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", request.getParameter("id"));
		map.put("status", request.getParameter("status"));
		groupService.groupAddUser(map);
	}
	 // 用户组设置角色
	@RequestMapping(value="/group_groupAddRole/{groupId}")
	public void groupAddRole(@PathVariable String groupId,HttpServletRequest request){
		Map<String, String> map = new HashMap<String, String>();
		String status = request.getParameter("status");
		if("true".equals(status)){
			map.put("ROLEID", request.getParameter("id"));
			map.put("GROUPID", groupId);
			if(!groupService.isGroupRole(map)){
				map.put("ID",new Integer(SequenceManager.nextID("group_role"))+"");
				groupService.groupAddRole(map);
			};

		}

	}
	 // 用户组设置角色
	@RequestMapping(value="/group_addOrUpdateGroup")
	public String enterAddOrUpdateGroup(HttpServletRequest request){
		return "manager/group/addOrUpdateGroup";
	}
	
	//保存或更新用户组
	@ResponseBody
	@RequestMapping(value="/group_saveOrUpdateGroup", method=RequestMethod.POST)
	public Map<String, Object> saveOrUpdateGroup(Group groupForm, HttpServletRequest request) {
		System.out.println("2012年9月13日22:29:03 ："+JSONObject.fromObject(groupForm).toString());
		groupService.saveOrUpdateGroup(groupForm);
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("success","true");
		jsonMap.put("msg","注册成功！");
		return jsonMap;
	}
	//删除用户组信息 如果包含子记录则不能删除
	@ResponseBody
	@RequestMapping(value="/group_deleteGroup/{groupId}")
	public Map<String, Object> enterShopList(@PathVariable String groupId, HttpServletRequest request) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		//if (!groupService.isOrderFood(shopId)) {
			if (groupService.deleteGroup(groupId)) {
				jsonMap.put("success", true);
				jsonMap.put("msg", "删除商家成功！");
			} else {
				jsonMap.put("success", true);
				jsonMap.put("msg", "删除商家成功！");
			}
//		} else {
//			jsonMap.put("success", false);
//			jsonMap.put("msg", "商家包含食品记录和订单记录，请删除后再删除商家！");
//		}
		
		return jsonMap;
	}
	
	/*** 后台管理 END ***/
}